package com.lxu.controller;

import com.lxu.entity.ReportDataEntity;
import com.lxu.service.WordTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

@Controller
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordTemplateService wordTemplateService;

    @RequestMapping("/templateToWord")
    public void templateWord(ReportDataEntity reportDataEntity, HttpServletResponse response) throws Exception {
        String wordName = "word模板";
        // 告诉浏览器用什么软件可以打开此文件
        response.reset();// 清空输出流
        response.setHeader("content-Type", "application/msword");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String(wordName.getBytes("GB2312"), "8859_1") + ".docx");
        OutputStream out = response.getOutputStream();
        //读取word模板
        wordTemplateService.exportWordTemplate(out, reportDataEntity);
    }
}
